package com.tadpole.music.presenter.wechat;

import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.wechat.BindWeChatIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 绑定微信
 * Created By 孙岩
 * on 2020/12/4
 */
public class BindWeChatPresenter extends BasePresenter<BindWeChatIView> {
    /**
     * @param wx_app_openid openid
     * @param wx_unionid unionid
     * @param nickname 昵称
     * @param country 国家
     * @param language 语言
     * @param province 省份
     * @param city 城市
     * @param headimg_url 头像
     * @param sex 性别
     */
    @SuppressWarnings("unchecked")
    public void bindWeChat(String wx_app_openid,String wx_unionid,String nickname,String country,String language,String province,String city,String headimg_url,int sex){
        if(isViewAttached()){
            String url = UrlConfig.bindWeChatV2;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN,"");
            DataModel.request(ModelToken.POST_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .paramKey("wx_app_openid","wx_unionid","nickname","country","language","province","city","headimg_url","sex")
                    .paramValue(wx_app_openid,wx_unionid,nickname,country,language,province,city,headimg_url,sex+"")
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    getView().showBindWeChatResult();
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
